//
//  AnjiMainViewController.m
//  OASystem
//
//  Created by zhoupengfei on 15/11/15.
//  Copyright © 2015年 zpf. All rights reserved.
//

/*
 Exchange（件数）
 待办文件
 offiece（件数）
 待办工作
 whetherExchange（权限控制）
 1  显示 待收文件、已收文件、已发文件。 0 不显示 如下图：
 whetherSms（权限控制）
 1 显示短信平台  0 不显示
 */

#import "AnjiMainViewController.h"
#import "OADueInViewController.h"
#import "OASMSViewController.h"
#import "OAUpComingWorkController.h"
#import "OAToDoWorkViewController.h"
#import "OANoteViewController.h"
#import "OANewInViewController.h"
#import "OANewSendViewController.h"
#import "OAInBoxViewController.h"
#import "OASendMessageViewController.h"
#import "OAHomeEmailViewController.h"
#import "OAAddressViewController.h"
#import "HttpTools.h"
#import "BaseModel.h"
#import "OALoginViewController.h"
#import "AuthHelper.h"

@interface HomeModel : BaseModel

@property(nonatomic,copy)NSString *  exchange;
@property(nonatomic,copy)NSString *  ksmail;
@property(nonatomic,copy)NSString *  mail;
@property(nonatomic,copy)NSString *  offiece;
@property(nonatomic,copy)NSString *  whetherExchange;
@property(nonatomic,copy)NSString *  whetherSms;
@property(nonatomic,copy)NSString * read;
@end



@interface AnjiMainViewController ()<SangforSDKDelegate>
@property (nonatomic, strong) AuthHelper *helper;

@property (weak, nonatomic) IBOutlet UITableViewCell *firstCell;
@property(nonatomic,strong)HomeModel * homeModel;
@property (weak, nonatomic) IBOutlet UIView *daiBanView;
@property (weak, nonatomic) IBOutlet UIImageView *daibanImageView;
@property (weak, nonatomic) IBOutlet UILabel *daibanLabel;
@property (weak, nonatomic) IBOutlet UIButton *daibanBtn;


@property (weak, nonatomic) IBOutlet UIView *duanxinView;
@property (weak, nonatomic) IBOutlet UIButton *waitReceiveBtnNum;
@property (weak, nonatomic) IBOutlet UIButton *waitWorkBtnNum;
@property (weak, nonatomic) IBOutlet UIButton *otherWorkBtnNum;

@property (weak, nonatomic) IBOutlet UIButton *waitReadBtnNum;
@property (weak, nonatomic) IBOutlet UIView *alreadyDoneWorkView;//已办工作



- (IBAction)pushVCAction:(id)sender;
- (IBAction)logout:(id)sender;

@end


@implementation AnjiMainViewController



-(void)setupCorn:(UIButton*)btn{
    btn.layer.cornerRadius = 12;
    btn.layer.masksToBounds = YES;
    
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    NSArray *  array = @[@{@"userId":userId}];;
    [HttpTools requestWithURL:KExchmain arrays:array method:KHomePqwoiunu success:^(id JSON) {
        NSArray *tempArray  = JSON[@"data"];
        NSDictionary * dict = [tempArray firstObject];
        self.homeModel = [[HomeModel alloc] initWitDictionary:dict];
        [self setupSubView];
    } failure:^(NSError *error) {
        
    }];
    }

- (void)viewDidLoad {
short port = 443;
NSString *vpnIp =    @"61.175.232.5";  //vpn设备IP地址
    self.helper = [[AuthHelper alloc] initWithHostAndPort:vpnIp port:port delegate:self];

    [super viewDidLoad];
    
    self.title = @"主页";
    
    [self setupCorn:self.waitReceiveBtnNum];
    [self setupCorn:self.waitWorkBtnNum];
    [self setupCorn:self.waitReadBtnNum];
    [self setupCorn:self.otherWorkBtnNum];
    

    
}

-(void)setupSubView{
    
    if ([self.homeModel.whetherExchange integerValue] == 1) {
        self.firstCell.hidden = NO;
        self.alreadyDoneWorkView.hidden = YES;
        self.daiBanView.hidden = NO;
        self.daibanImageView.image = [UIImage imageNamed:@"已办工作"];
        self.daibanLabel.text = @"已办文件";
        self.otherWorkBtnNum.hidden = YES;
        self.daibanBtn.tag = 104;
    }else{
        self.daiBanView.hidden = NO;
        self.firstCell.hidden = YES;
        self.alreadyDoneWorkView.hidden = NO;
        self.otherWorkBtnNum.hidden = NO;
    }
    
    if ([self.homeModel.whetherSms integerValue] == 1) {
        self.duanxinView.hidden = NO;
    }else{
        self.duanxinView.hidden = YES;
    }
    
    if ([self.homeModel.exchange integerValue] != 0) {
        self.waitReceiveBtnNum.hidden = NO;
        [self.waitReceiveBtnNum setTitle:self.homeModel.exchange forState:UIControlStateNormal];
    }else{
        self.waitReceiveBtnNum.hidden = YES;
    }
    
    if ([self.homeModel.offiece integerValue] != 0) {
        self.waitWorkBtnNum.hidden = NO;
        [self.waitWorkBtnNum  setTitle:self.homeModel.offiece forState:UIControlStateNormal];
        [self.otherWorkBtnNum  setTitle:self.homeModel.offiece forState:UIControlStateNormal];
    }else{
        self.waitWorkBtnNum.hidden = YES;
    }
    
    if ([self.homeModel.read integerValue] != 0) {
        self.waitReadBtnNum.hidden = NO;
        [self.waitReadBtnNum  setTitle:self.homeModel.read forState:UIControlStateNormal];
    }else{
        self.waitReadBtnNum.hidden = YES;
    }
    
    
    [self.tableView reloadData];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.row == 0 && self.firstCell.hidden) {
        return 0;
    }
    return 105;
}

- (IBAction)pushVCAction:(id)sender {
    UIButton * btn = (UIButton*)sender;
     UIStoryboard * storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    switch (btn.tag) {
        case 100:{ //待收文件
            OADueInViewController * dueInVC = (OADueInViewController*)[self.storyboard instantiateViewControllerWithIdentifier:@"DueInVC"];
            dueInVC.vcType = WaitForSignType;
            [self.navigationController pushViewController:dueInVC animated:YES];
            break;
        }
        case 101:{ //待收文件
            OADueInViewController * dueInVC = (OADueInViewController*)[self.storyboard instantiateViewControllerWithIdentifier:@"DueInVC"];
            dueInVC.vcType = AlreadySignType;
            [self.navigationController pushViewController:dueInVC animated:YES];
            break;
        }
        case 102:{ //待收文件
            OADueInViewController * dueInVC = (OADueInViewController*)[self.storyboard instantiateViewControllerWithIdentifier:@"DueInVC"];
            dueInVC.vcType = SenderSinType;
            [self.navigationController pushViewController:dueInVC animated:YES];
            break;
        }
        case 103:{ //待办工作
           
            OAUpComingWorkController * upcomingWorkVC  = (OAUpComingWorkController*)[storyBoard instantiateViewControllerWithIdentifier:@"UpComingWorkVCID"];
            [self.navigationController pushViewController:upcomingWorkVC animated:YES];
            break;
        }
        case 104:{ //已办文件
            
            OAToDoWorkViewController * todoWorkVC  = (OAToDoWorkViewController*)[storyBoard instantiateViewControllerWithIdentifier:@"TodoWorkVCID"];
            [self.navigationController pushViewController:todoWorkVC animated:YES];
            break;
        } //
        case 105:{ //电子公告
            OANoteViewController * noteVC  = (OANoteViewController*)[storyBoard instantiateViewControllerWithIdentifier:@"noteVCID"];
            [self.navigationController pushViewController:noteVC animated:YES];
            break;
        }
        case 106:{ //最新收文
            OANewInViewController * newInVC  = (OANewInViewController*)[storyBoard instantiateViewControllerWithIdentifier:@"newInVCID"];
            [self.navigationController pushViewController:newInVC animated:YES];
            break;
        }
        case 107:{ //最新发文
            OANewSendViewController * sendVC  = (OANewSendViewController*)[storyBoard instantiateViewControllerWithIdentifier:@"sendVCID"];
            [self.navigationController pushViewController:sendVC animated:YES];
            break;
        }
        case 108:{ //科室收件箱
            OAInBoxViewController * inBoxVC  = (OAInBoxViewController*)[storyBoard instantiateViewControllerWithIdentifier:@"InBoxVCID"];
            inBoxVC.isKeShi = YES;
            [self.navigationController pushViewController:inBoxVC animated:YES];
            break;
        }
        case 109:{ //科室发件箱
            OASendMessageViewController * sendVC  = (OASendMessageViewController*)[storyBoard instantiateViewControllerWithIdentifier:@"SendMessageVCID"];
            sendVC.isKeShi = YES;
            [self.navigationController pushViewController:sendVC animated:YES];
            break;
        }
        case 110:{ //个人收件箱
            OAInBoxViewController * inBoxVC  = (OAInBoxViewController*)[storyBoard instantiateViewControllerWithIdentifier:@"InBoxVCID"];
            [self.navigationController pushViewController:inBoxVC animated:YES];
            break;
        }
        case 111:{ //个人发件箱
            OASendMessageViewController * sendVC  = (OASendMessageViewController*)[storyBoard instantiateViewControllerWithIdentifier:@"SendMessageVCID"];
            [self.navigationController pushViewController:sendVC animated:YES];
            break;
        }
        case 112:{ //短息系统
            OASMSViewController * smsVC = (OASMSViewController*)[self.storyboard instantiateViewControllerWithIdentifier:@"SMSVCID"];
            smsVC.vcType = SMSViewControllerType;
            [self.navigationController pushViewController:smsVC animated:YES];
            break;
        }
        case 113:{ //通讯录
            OAAddressViewController * addressVC = (OAAddressViewController*)[self.storyboard instantiateViewControllerWithIdentifier:@"AddressVCID"];
            [self.navigationController pushViewController:addressVC animated:YES];
            break;
        }
        case 114:{ //邮件集合
            OAHomeEmailViewController * homeEmailVC = (OAHomeEmailViewController*)[self.storyboard instantiateViewControllerWithIdentifier:@"HomeEmailID"];
            [self.navigationController pushViewController:homeEmailVC animated:YES];
            break;
        }
        case 115:{ //待办工作
            OAUpComingWorkController * upcomingWorkVC  = (OAUpComingWorkController*)[storyBoard instantiateViewControllerWithIdentifier:@"UpComingWorkVCID"];
            [self.navigationController pushViewController:upcomingWorkVC animated:YES];
            break;
        }
            case 116://待阅文件
        {
        
            OADueInViewController * dueInVC = (OADueInViewController*)[self.storyboard instantiateViewControllerWithIdentifier:@"DueInVC"];
            dueInVC.vcType = WaitForReadType;
            [self.navigationController pushViewController:dueInVC animated:YES];
        }
        default:
            break;
    }
    
}

- (IBAction)logout:(id)sender {
//     [_helper logoutVpn];

    OALoginViewController * loginVC =  [self.storyboard instantiateViewControllerWithIdentifier:@"LoginVC"];
     [[NSUserDefaults standardUserDefaults] setObject:@"N" forKey:@"autoLogin"];
         loginVC.type = @"1";
    self.view.window.rootViewController = loginVC;
    
}
@end
